package com.itheima.dubbo.mongodb.impl;


import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.pojo.RecommendUser;
import com.itheima.commons.pojo.UserInfo;
import com.itheima.commons.vo.PageResult;
import com.itheima.commons.vo.TodayBest;
import com.itheima.dubbo.interfaces.RecommendUserApi;
import com.itheima.dubbo.interfaces.UserInfoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * @param id 此id为用户id，
     * @return userId 为今日佳人id
     */

    @Override
    public RecommendUser findRecommendUserById(Long id) {
        //找一个匹配分数最高的id
        //根据匹配分数获取第一个
        double minDistance = 0;
        RecommendUser one = mongoTemplate.findOne(Query.query(Criteria.where("toUserId").is(id))
                        .with(Sort.by(Sort.Order.desc("score")))
                        .with(PageRequest.of(0, 1))
                , RecommendUser.class);
        return one;
    }

    //分页查询当前用户id的推荐用户，分数不用排序
    @Override
    public PageResult findAllRecommendUsersById(Integer size, Integer page, Long id) {
        List<RecommendUser> recommendUserList = mongoTemplate.find(Query.query(Criteria.where("toUserId").is(id))
                        .with(PageRequest.of(page - 1, size))
                , RecommendUser.class);
        return new PageResult(page, size, Convert.toLong(recommendUserList.size()), recommendUserList);
    }

    @Override
    public RecommendUser queryScore(Long myId, Long userId) {
        return mongoTemplate.findOne(Query.query(Criteria.where("toUserId").is(myId).and("userId").is(userId)), RecommendUser.class);


    }
}
